package org.huiqiang.fill;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;

/**
 * @author caohq
 * @date 2021/3/22 22:21
 */
public class FillTest {

    public static void main(String[] args) {
        EventQueue.invokeLater(()->{
            JFrame jFrame = new FillFrame();
            jFrame.setTitle("FillTest");
            jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jFrame.setVisible(true);
        });
    }
}

class FillFrame extends JFrame{
    public FillFrame() {
        add(new FillComponent());
        pack();
    }
}

class FillComponent extends JComponent {
    private static final int DEFAULT_WIDTH = 400;
    private static final int DEFAULT_HEIGHT = 400;

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        double leftX = 100;
        double topY = 100;
        double width = 200;
        double height = 150;

        Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
        g2.setPaint(Color.black);
        g2.fill(rect);
        g2.setPaint(Color.red);
        g2.fill(rect);

        Ellipse2D ellipse = new Ellipse2D.Double(leftX, topY, width, height);
        ellipse.setFrame(rect);
        g2.setPaint(new Color(0, 128, 128));
        g2.fill(ellipse);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    }
}
